package com.wms.presenters;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import com.gatech.wms.R;
import com.wms.assets.Categories;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;


public class FilterScreenActivity extends WMSActivity {
	
	private Map<String, Boolean> categories;
	
	private int year = -1;
	private int month = -1;
	private int day = -1;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_screen);
		categories = new HashMap<String, Boolean>();
		categories.put(Categories.ACCESSORIES, false);
		categories.put(Categories.ANIMALS, false);
		categories.put(Categories.CLOTHING, false);
		categories.put(Categories.ELECTRONICS, false);
		categories.put(Categories.TOYS, false);
		categories.put(Categories.MISCELLANY, false);
		setupButtons();
	}
	
	private void setupButtons() {
		
		Button btnClick = (Button)findViewById(R.id.button3);
		btnClick.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Calendar c = Calendar.getInstance();
				int year = c.get(Calendar.YEAR);
				int month = c.get(Calendar.MONTH);
				int day = c.get(Calendar.DAY_OF_MONTH);
				DatePickerDialog time = new DatePickerDialog(FilterScreenActivity.this, new DateListener(), year, month, day);
				time.show();
			}
		});
		
		Button cont = (Button)findViewById(R.id.button1);
		cont.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CheckBox foundChk = (CheckBox)findViewById(R.id.foundChk);
				Intent i = getIntent();
				i.putExtra(Categories.CLOTHING, categories.get(Categories.CLOTHING));
				i.putExtra(Categories.ACCESSORIES, categories.get(Categories.ACCESSORIES));
				i.putExtra(Categories.TOYS, categories.get(Categories.TOYS));
				i.putExtra(Categories.ELECTRONICS, categories.get(Categories.ELECTRONICS));
				i.putExtra(Categories.ANIMALS, categories.get(Categories.ANIMALS));
				i.putExtra(Categories.MISCELLANY, categories.get(Categories.MISCELLANY));
				i.putExtra("year", year);
				i.putExtra("day", day);
				i.putExtra("month", month);
				i.putExtra("found", foundChk.isChecked());
				FilterScreenActivity.this.setResult(15, getIntent());
        		FilterScreenActivity.this.finish();
			}
		});
		
		Button cancel = (Button)findViewById(R.id.button2);
		cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FilterScreenActivity.this.finish();
			}
		});
		
		CheckBox clothing = (CheckBox) findViewById(R.id.clothing);
		CheckBox jewelry = (CheckBox) findViewById(R.id.jewelry);
		CheckBox toys = (CheckBox) findViewById(R.id.toys);
		CheckBox electronics = (CheckBox) findViewById(R.id.electronics);
		CheckBox animals = (CheckBox) findViewById(R.id.animals);
		CheckBox other = (CheckBox) findViewById(R.id.other);
		
		clothing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				clothingCheck(isChecked);
			}
		});
		jewelry.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				jewelryCheck(isChecked);
			}
		});
		toys.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				toysCheck(isChecked);
			}
		});
		electronics.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				elecCheck(isChecked);
			}
		});
		animals.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				animalCheck(isChecked);
			}
		});
		other.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				otherCheck(isChecked);
			}
		});
	}
	
	private class DateListener implements DatePickerDialog.OnDateSetListener {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			FilterScreenActivity.this.year = year;
			month = monthOfYear;
			day = dayOfMonth;
		}
	}
	
	private void clothingCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.CLOTHING, true);
		else
			categories.put(Categories.CLOTHING, false);
	}
	private void jewelryCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.ACCESSORIES, true);
		else
			categories.put(Categories.ACCESSORIES, false);
	}
	private void toysCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.TOYS, true);
		else
			categories.put(Categories.TOYS, false);
	}
	private void elecCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.ELECTRONICS, true);
		else
			categories.put(Categories.ELECTRONICS, false);
	}
	private void animalCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.ANIMALS, true);
		else
			categories.put(Categories.ANIMALS, false);
	}
	private void otherCheck(Boolean isChecked){
		if(isChecked)
			categories.put(Categories.MISCELLANY, true);
		else
			categories.put(Categories.MISCELLANY, false);
	}
}
